<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width"/>
    <title>工程师绑定</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
</head>
<body>
<h2 style="color:#666;text-align:center;">维修工程师绑定</h2>

<div class="weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="rname"
                   placeholder="请输入姓名"/>
        </div>
        <div class="weui-cell__ft">
            <i class="weui-icon-warn"></i>
        </div>
    </div>
    <div class="weui-cell weui-cell_vcode">
        <div class="weui-cell__hd">
            <label class="weui-label">手机号</label>
        </div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="tel"
                   type="number" placeholder="请输入手机号"/>
        </div>
        <div class="weui-cell__ft">
            <button class="weui-vcode-btn">获取验证码</button>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">验证码</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="number" id="yzm"
                   placeholder="请输入验证码"/>
        </div>
    </div>
</div>

<a href="javascript:;" onclick="bangding()" style="width:90%;margin-top:20px;" class="weui-btn weui-btn_primary">绑定</a>

<a href="javascript:;" onclick="location='equipment_add.html'"
   style="width:90%;margin-top:20px;" class="weui-btn weui-btn_primary">设备录入</a>

<!-- body 最后 -->
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>

<!-- 如果使用了某些拓展插件还需要额外的JS -->
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/swiper.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/city-picker.min.js"></script>

<script>
    $.showLoading("正在加载...");
    var openid = "";
    $(function () {
        var url = document.URL;
       // alert(url);
        var start = url.indexOf("code=");
        var end = url.indexOf("&");
        var code = url.substring(start + 5, end);

        $.get("/find_openid", {code: code}, function (data) {
            //   alert(data);
            openid = data;

            localStorage.openid = openid;

            $.get("/find_repairman_byopenid", {openid: openid}, function (data) {
                if (data) {
                    localStorage.repairMan = JSON.stringify(data);
                    location = 'repair_main.html';
                }else{
                    $.hideLoading();
                }
            });

        })
    });
</script>

<script>


    function check() {
        if (rname.value.trim().length == 0) {
            $("#rname").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        if (tel.value.trim().length == 0) {
            $.toast("手机号必填哦，亲", "text");
            $("#tel").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        var reg = /1[3-9]\d{9}/;
        if (!reg.test($("#tel").val())) {
            $.toast("手机号格式不对哦，亲", "text");
            $("#tel").parents(".weui-cell").addClass("weui-cell_warn");
            return false;
        }

        return true;
    }

    $(function () {
        var codeBtnOK = true;
        $(".weui-vcode-btn").click(function () {

            if (codeBtnOK) {

                $.get("sendsms", {tel: tel.value}, function (data) {
                    if (data) {
                        $.toast("短信已经发送！");
                    }

                });

                var t = 5;
                codeBtnOK = false;
                $(this).text(t + "秒后重新发送");

                var timer = setInterval(function () {
                        $(".weui-vcode-btn").text(--t + "秒后重新发送");
                        if (t == 1) {
                            clearInterval(timer);
                            codeBtnOK = true;
                            $(".weui-vcode-btn").text("获取验证码");
                        }
                    },
                    1000)
            }


        });
        $("#rname,#tel").focus(function () {
            $(this).parents(".weui-cell").removeClass("weui-cell_warn");
        });

        $("#rname,#tel").blur(function () {
            if ($(this).val().trim().length == 0) {
                $(this).parents(".weui-cell").addClass("weui-cell_warn");
                return;
            }

            var $p = $(this);
            $.get("tel_isrepeat", {tel: tel.value}, function (data) {
                if (!data) {
                    $.toast("这个号码已经绑定！", "cancel")
                    $p.parents(".weui-cell").addClass("weui-cell_warn");
                    return;
                }
            })
        });


    });

    function bangding() {
        if (!check()) {
            return;
        }

        $.ajax({
            type: 'post',
            url: 'repair_man_bind',
            data: JSON.stringify({
                name: rname.value,
                tel: tel.value,
                openid: openid,
                yzm: yzm.value
            }),
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                if (data) {
                    $.toast("谢谢您的绑定！")
                    localStorage.repairMan = JSON.stringify(data);
                    location = 'repair_main.html'

                } else {
                    $.toast("绑定失败，请您重试！", "cancel")
                }

            }
        });
    }
</script>
</body>
</html>